<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/9/22
 * Time: 16:16
 */

namespace Wechat\Controller;

//新本个人中心控制器
class WebmineController extends CommonController
{
    //收藏
    public function shoucang()
    {
        $id = i('get.id');
        $model = i('get.model');
        $data = [
            'time'=>date("Y-m-d H:i",time()),
            'openid'=>$_SESSION['wechatopenid'],
            'tid'=>$id,
            'model'=>$model
        ];
        if(M('webshoucang')->add($data)){
            $this->success('收藏成功！');
        }else{
            $this->error('失败！');
        }
    }
    //取消收藏
    public function quxiaoshoucang()
    {
        $id = i('get.id');
        $model = i('get.model');
        if(M('webshoucang')->where(['id'=>$id,'model'=>$model])->delete()){
            $this->success('取消成功！');
        }else{
            $this->error('失败！');
        }
    }
    //我的主页
    public function index()
    {
        $data = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $f = M('webbuy')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $this->assign(['data'=>$data,'f'=>$f]);
        $this->display();
    }
    //我的收藏
    public function mineshoucang()
    {
        $this->display();
    }
    //我的发布
    public function fabu()
    {
        $this->display();
    }
    //我要发布-出租
    public function chuzu()
    {
        $data = M('zhuanzulin')->where(['build'=>1,'openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我要发布-求组
    public function qiuzu()
    {
        $data = M('zhuanzulin')->where(['build'=>2,'openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我要发布-新房
    public function xinfang()
    {
        $data = M('zhuanxinfang')->where(['openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我要发布-求买
    public function qiumai()
    {
        $data = M('zhuanershou')->where(['build'=>2,'openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我要发布-出售
    public function chushou()
    {
        $data = M('zhuanershou')->where(['build'=>1,'openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我要发布-单位
    public function danwei()
    {
        $data = M('zhuandanwei')->where(['openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我要发布-团队
    public function tuandui()
    {
        $data = M('zhuanxiangmu')->where(['build'=>1,'openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我要发布-项目
    public function xiangmu()
    {
        $data = M('zhuanxiangmu')->where(['build'=>2,'openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我要发布-其他
    public function qita()
    {
        $data = M('zhuanqita')->where(['openid'=>$_SESSION['wechatopenid']])->order('time desc')->select();
        echo json_encode($data);
    }
    //我的收藏所有
    public function shoucangmine()
    {
        $data = M('webshoucang')->where(['openid'=>$_SESSION['wechatopenid']])->select();
        foreach ($data as $k => $v){
            $data[$k]['tid'] = M($v['model'])->where(['id'=>$v['tid']])->select();
        }
        echo json_encode($data);
    }
    //我的发布-新房删除
    public function xinfangdel()
    {
        $id = i('get.id');
        if(M('zhuanxinfang')->where(['id'=>$id])->delete()){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
    //我的发布-租赁删除
    public function zulindel()
    {
        $id = i('get.id');
        if(M('zhuanzulin')->where(['id'=>$id])->delete()){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
    //我的发布-其他删除
    public function qitadel()
    {
        $id = i('get.id');
        if(M('zhuanqita')->where(['id'=>$id])->delete()){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
    //我的发布-顶置新房
    public function xinfangding()
    {
        $id = i('get.id');
        $data = M('zhuanxinfang')->where(['id'=>$id])->save(['sort'=>1]);
        if($data){
            $this->success('顶置成功！');
        }else{
            $this->error('失败！');
        }
    }
    //我的发布-其他新房
    public function qitading()
    {
        $id = i('get.id');
        $data = M('zhuanqita')->where(['id'=>$id])->save(['sort'=>1]);
        if($data){
            $this->success('顶置成功！');
        }else{
            $this->error('失败！');
        }
    }
    //我的发布-租赁新房
    public function zulinding()
    {
        $id = i('get.id');
        $data = M('zhuanzulin')->where(['id'=>$id])->save(['sort'=>1]);
        if($data){
            $this->success('顶置成功！');
        }else{
            $this->error('失败！');
        }
    }
    //求组编辑
    public function qiuzued()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['des']))) {
                $this->error('请填写描述');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'build'=>$post['build'],
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'huxing'=>$post['huxing'],
                'fangling'=>$post['fangling'],
                'louceng'=>$post['louceng'],
                'mianji'=>$post['mianji'],
                'zhuangxiu'=>$post['zhuangxiu'],
                'yuezu'=>$post['yuezu'],
                'fukuan'=>$post['fukuan'],
                'fenyong'=>$post['fenyong'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'phone'=>$post['phone'],
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid'],
                'read'=>0,
                'where'=>$post['where'],
                'where1'=>$post['where1'],
                'headimg'=>$user['headimg'],
            ];
            if(M('zhuanzulin')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuanzulin')->where(['id'=>$id])->find();
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //出租编辑
    public function chuzued()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['des']))) {
                $this->error('请填写描述');
            }
            if(trim(empty($post['thumb']))) {
                $this->error('请上传图片');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'build'=>$post['build'],
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'huxing'=>$post['huxing'],
                'fangling'=>$post['fangling'],
                'louceng'=>$post['louceng'],
                'mianji'=>$post['mianji'],
                'zhuangxiu'=>$post['zhuangxiu'],
                'yuezu'=>$post['yuezu'],
                'fukuan'=>$post['fukuan'],
                'fenyong'=>$post['fenyong'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'phone'=>$post['phone'],
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid'],
                'read'=>0,
                'where'=>$post['where'],
                'where1'=>$post['where1'],
                'headimg'=>$user['headimg'],
            ];
            if(M('zhuanzulin')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuanzulin')->where(['id'=>$id])->find();
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //二手房出售编辑
    public function chushoued()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['buildname']))) {
                $this->error('请填写小区名称/商圈');
            }
            if(trim(empty($post['des']))) {
                $this->error('请填写描述');
            }
            if(trim(empty($post['thumb']))) {
                $this->error('请上传图片');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'build'=>$post['build'],
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'huxing'=>$post['huxing'],
                'fangling'=>$post['fangling'],
                'louceng'=>$post['louceng'],
                'mianji'=>$post['mianji'],
                'zhuangxiu'=>$post['zhuangxiu'],
                'jiage'=>$post['jiage'],
                'fenyong'=>$post['fenyong'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'phone'=>$post['phone'],
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid'],
                'read'=>0,
                'where'=>$post['where'],
                'where1'=>$post['where1'],
                'headimg'=>$user['headimg'],
            ];
            if(M('zhuanershou')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuanershou')->where(['id'=>$id])->find();
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //团队编辑
    public function tuanduied()
    {
        $id = i('get.id');
        $data = M('zhuanxiangmu')->where(['id'=>$id])->find();
        $this->assign(['vo'=>$data]);
        $this->display();
    }
    //团队编辑  项目编辑
    public function xiangmued()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['phone']))) {
                $this->error('请输入发布者手机号');
            }
            if(trim(empty($post['des']))) {
                $this->error('请输入详细描述');
            }
            if(trim(empty($post['name']))) {
                $this->error('请输入发布人姓名');
            }
            $data=[
                'build'=>$post['build'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'renshu'=>$post['renshu'],
                'thumb'=>$post['thumb'],
                'phone'=>$post['phone'],
                'time'=>time(),
                'des'=>$post['des'],
                'type'=>$post['type'],
                'name'=>$post['name'],
                'zhuchang'=>$post['zhuchang'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'jiage'=>$post['jiage'],
                'ticheng'=>$post['ticheng'],
                'fanyong'=>$post['fanyong'],
                'read'=>0,
                'openid'=>$_SESSION['wechatopenid']
            ];
            if(M('zhuanxiangmu')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuanxiangmu')->where(['id'=>$id])->find();
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //其他编辑
    public function qitaed()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['tel']))) {
                $this->error('请输入相关业务手机号');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入发布者手机号');
            }
            if(trim(empty($post['des']))) {
                $this->error('请输入详细描述');
            }
            if(trim(empty($post['name']))) {
                $this->error('请输入发布人姓名');
            }
            if(trim(empty($post['compname']))) {
                $this->error('请输入公司名称');
            }
            //装修的是1  搬家的是2
            $data = [
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'compname'=>$post['compname'],
                'fenyong'=>$post['fenyong'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'tel'=>$post['tel'],
                'compthumb'=>$post['compthumb'],
                'phone'=>$post['phone'],
                'read'=>0,
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid']
            ];
            if(M('zhuanqita')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuanqita')->where(['id'=>$id])->find();
            //var_dump($data);
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //招聘编辑
    public function danweied()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['mingcheng']))) {
                $this->error('请填写岗位名称');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            //装修的是1  搬家的是2
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'yingye'=>$post['yingye'],
                'mingcheng'=>$post['mingcheng'],
                'renshu'=>$post['renshu'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'erwei'=>$post['erwei'],
                'phone'=>$post['phone'],
                'name'=>$post['name'],
                'compname'=>$post['compname'],
                'time'=>time(),
                'read'=>0,
                'openid'=>$_SESSION['wechatopenid'],
                'headimg'=>$user['headimg'],
            ];
            if(M('zhuandanwei')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuandanwei')->where(['id'=>$id])->find();
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //二手房求买编辑
    public function qiumaied()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['buildname']))) {
                $this->error('请填写小区名称/商圈');
            }
            if(trim(empty($post['des']))) {
                $this->error('请填写描述');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'build'=>$post['build'],
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'huxing'=>$post['huxing'],
                'fangling'=>$post['fangling'],
                'louceng'=>$post['louceng'],
                'mianji'=>$post['mianji'],
                'zhuangxiu'=>$post['zhuangxiu'],
                'jiage'=>$post['jiage'],
                'fenyong'=>$post['fenyong'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'phone'=>$post['phone'],
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid'],
                'read'=>0,
                'where'=>$post['where'],
                'where1'=>$post['where1'],
                'headimg'=>$user['headimg'],
            ];
            if(M('zhuanershou')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuanershou')->where(['id'=>$id])->find();
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //新房编辑
    public function xinfanged()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['buildname']))) {
                $this->error('请填写小区名称/商圈');
            }
            if(trim(empty($post['des']))) {
                $this->error('请填写描述');
            }
            if(trim(empty($post['thumb']))) {
                $this->error('请上传图片');
            }
            if(trim(empty($post['tel']))) {
                $this->error('请输入业务联系号码');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            if(trim(empty($post['jiage']))) {
                $this->error('请输入价格');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'jiage'=>$post['jiage'],
                'yongjin'=>$post['yongjin'],
                'fanyong'=>$post['fanyong'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'mianji'=>$post['mianji'],
                'erwei'=>$post['erwei'],
                'tel'=>$post['tel'],
                'phone'=>$post['phone'],
                'read'=>0,
                'time'=>time(),
                'headimg'=>$user['headimg'],
                'where'=>$post['where'],
                'openid'=>$_SESSION['wechatopenid']
            ];
            if(M('zhuanxinfang')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->error('修改失败！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuanxinfang')->where(['id'=>$id])->find();
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //其他装修编辑
    public function zhuangxiued()
    {
        $id = i('get.id');
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['quyu']))) {
                $this->error('请选择区域');
            }
            if(trim(empty($post['thumb']))) {
                $this->error('请上传相关图片内容');
            }
            if(trim(empty($post['tel']))) {
                $this->error('请输入相关业务手机号');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入发布者手机号');
            }
            if(trim(empty($post['des']))) {
                $this->error('请输入详细描述');
            }
            if(trim(empty($post['name']))) {
                $this->error('请输入发布人姓名');
            }
            if(trim(empty($post['compname']))) {
                $this->error('请输入公司名称');
            }
            if(trim(empty($post['compthumb']))) {
                $this->error('请上传公司营业执照');
            }
            $data = [
                'type'=>1,
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'compname'=>$post['compname'],
                'fenyong'=>$post['fenyong'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'tel'=>$post['tel'],
                'compthumb'=>$post['compthumb'],
                'phone'=>$post['phone'],
                'read'=>0,
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid']
            ];
            if(M('zhuanqita')->where(['id'=>$id])->save($data)){
                $this->success('修改成功！',U('Webmine/fabu'));
            }else{
                $this->success('修改失败！！');
            }
        }else{
            $id = i('get.id');
            $data = M('zhuanqita')->where(['id'=>$id])->find();
            $this->assign(['vo'=>$data]);
            $this->display();
        }
    }
    //其他搬家编辑
    public function banjiaed()
    {
        $id = i('get.id');
        $post = i('post.');
        if(trim(empty($post['quyu']))) {
            $this->error('请选择区域');
        }
        if(trim(empty($post['thumb']))) {
            $this->error('请上传相关图片内容');
        }
        if(trim(empty($post['tel']))) {
            $this->error('请输入相关业务手机号');
        }
        if(trim(empty($post['phone']))) {
            $this->error('请输入发布者手机号');
        }
        if(trim(empty($post['des']))) {
            $this->error('请输入详细描述');
        }
        if(trim(empty($post['name']))) {
            $this->error('请输入发布人姓名');
        }
        if(trim(empty($post['compname']))) {
            $this->error('请输入公司名称');
        }
        if(trim(empty($post['compthumb']))) {
            $this->error('请上传公司营业执照');
        }
        $data = [
            'type'=>2,
            'chengshi'=>$post['chengshi'],
            'quyu'=>$post['quyu'],
            'thumb'=>$post['thumb'],
            'des'=>$post['des'],
            'compname'=>$post['compname'],
            'fenyong'=>$post['fenyong'],
            'name'=>$post['name'],
            'erwei'=>$post['erwei'],
            'tel'=>$post['tel'],
            'phone'=>$post['phone'],
            'compthumb'=>$post['compthumb'],
            'read'=>0,
            'time'=>time(),
            'openid'=>$_SESSION['wechatopenid']
        ];
        if(M('zhuanqita')->where(['id'=>$id])->add($data)){
            $this->success('修改成功！',U('Webmine/fabu'));
        }else{
            $this->success('修改失败！！');
        }
    }
    //搜索
    public function sousuo()
    {
        $this->display();
    }
    //历史搜索接口
    public function lishi()
    {
        $data = M('sousuo')->where(['openid'=>$_SESSION['wechatopenid']])->order('id desc')->limit(8)->select();
        echo json_encode($data);
    }
    //热门搜索
    public function remen()
    {
        $data = M('sousuo')->order('time desc')->limit(8)->select();
        echo json_encode($data);
    }
    //搜索返回数据
    public function sousuofan()
    {
        if(IS_POST){
            //获取提交的数据
            $ser = i('post.ser');
            $list = i('post.list');
            $where= array('like','%'.$ser.'%');
            //历史搜索
            $data1 = [
                'name'=>$ser,
                'openid'=>$_SESSION['wechatopenid'],
                'time'=>0
            ];
            //添加搜索次数
            $find = M('sousuo')->where(['name'=>$where])->find();
            $save = $find['time']+1;
            //如果可以找到的话就增加次数
            //要不就增加一条
            if($find){
                M('sousuo')->where(['id'=>$find['id']])->save(['time'=>$save]);
            }else{
                M('sousuo')->add($data1);
            }

            if($list==1){
                $xinfang = M('zhuanxinfang')->where(['buildname'=>$where])->select();
                $data = [
                    'xinfang'=>$xinfang,
                ];
            }
            if($list==2){
                $zulin = M('zhuanzulin')->where(['buildname'=>$where])->select();
                $data = [
                    'xinfang'=>$zulin,
                ];
            }
            if($list==3){
                $ershou = M('zhuanershou')->where(['buildname'=>$where])->select();
                $data = [
                    'xinfang'=>$ershou,
                ];
            }
            //如果找不到去掉空数组
            foreach ($data as $k => $v){
                if(!$v){
                    unset($data[$k]);
                }
            }

            $this->assign(['data'=>$data,'zulin'=>$zulin,'ershou'=>$ershou,'xinfang'=>$xinfang]);
            $this->display();
        }else{
            $this->display();
        }
    }
}